package com.dinglit.controller;

import com.dinglit.domain.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/***
 *
 *
 * @author liuhuanchao
 * @date 2019-08-22 
 */
@RestController
@RequestMapping(value = "/users")
public class UserController {


    static Map<Long, User> users = new LinkedHashMap<>();

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public Long addUser(@ModelAttribute User user) {
        users.put(user.getUserId(), user);
        return user.getUserId();
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Long removeUser(@PathVariable Long userId) {
        users.remove(userId);
        return userId;
    }

    /**
     * 修改用户信息
     *
     * @param userId
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateUser(@PathVariable Long userId, @ModelAttribute User user) {
        users.put(userId, user);
        return "success";
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable Long userId) {
        User user = users.get(userId);
        return user;
    }

    /**
     * 获取用户信息集合
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<User> getListUser() {
        List<User> userList = new ArrayList<>(users.values());
        return userList;
    }
}
